package com.dxc.intern.controller;


import com.dxc.intern.entity.Post;
import com.dxc.intern.entity.Result;
import com.dxc.intern.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangchengming
 * @since 2022-02-21
 */
@CrossOrigin
@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostService postService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody Post post){
        boolean b=postService.addPost(post);
        return b ? Result.success("添加成功", true) :
                Result.error("添加失败", false);
    }

    @DeleteMapping("/delete")
    public Result<String> delete(@RequestParam(name = "id") Integer id){
        boolean b=postService.deletePost(id);
        return b ? Result.success("删除成功", postService.toString()) :
                Result.error("删除失败", "false");
    }

    @PutMapping("/update")
    public Result<String> update(@RequestBody Post post){
        boolean b=postService.updatePost(post);
        return b ? Result.success("更新成功",post.toString()) :
                Result.error("更新失败", post.toString());

    }

}
